	
	package Dao.daoimpl;
	
	import java.sql.Connection;
	import java.sql.ResultSet;
	import java.sql.SQLException;
	import java.sql.Statement;
	import java.util.ArrayList;
	import java.util.List;
	
	import unitls.DataBaseConnection;
	import enitys.Course;
	import Dao.CouserDao;
	
	public class CourseDaoImpl implements CouserDao {
		private Connection conn = null;
		private Statement stmt = null;
		private ResultSet rs = null;
		private String sql = null;// sql语句
		
		public List<Course> courseallinfo() throws Exception {
			// TODO Auto-generated method stub
	
			this.sql = "select * from course";
			List<Course> info = new ArrayList<Course>();
			this.conn = DataBaseConnection.getConnection();
			try {
				this.stmt = this.conn.createStatement();
				this.rs = this.stmt.executeQuery(this.sql);
				// 把信息放进List
				while (this.rs.next()) {
					Course temp = new Course();
					temp.setC_id(this.rs.getInt("c_id"));
					temp.setC_name(this.rs.getString("c_name"));
					temp.setTeacher(this.rs.getString("teacher"));
					temp.setScore(this.rs.getString("score"));
					info.add(temp);
				}
				return info;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeRs(this.rs);
				DataBaseConnection.closeStmt(this.stmt);
				DataBaseConnection.closeConn(this.conn);
			}
			//若失败返回null
			return null;
		}
		
		@Override
		public List<Course> findCourseInfoById(int id) {
			// TODO Auto-generated method stub
			sql="select * from course where c_id="+id;
			List<Course> info = new ArrayList<Course>();
			this.conn = DataBaseConnection.getConnection();
			try {
				this.stmt = this.conn.createStatement();
				this.rs = this.stmt.executeQuery(this.sql);
				// 把信息放进List
				while (this.rs.next()) {
					Course temp = new Course();
					temp.setC_id(this.rs.getInt("c_id"));
					temp.setC_name(this.rs.getString("c_name"));
					temp.setTeacher(this.rs.getString("teacher"));
					temp.setScore(this.rs.getString("score"));
					info.add(temp);
				}
				return info;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeRs(this.rs);
				DataBaseConnection.closeStmt(this.stmt);
				DataBaseConnection.closeConn(this.conn);
			}
			//若失败返回null
			return null;
		}
	
		@Override
		public boolean AddCourseInfo(Course course) {
			// TODO Auto-generated method stub
			this.sql = "select * from course where c_id = '" + course.getC_id() + "'";
			this.conn = DataBaseConnection.getConnection();
			try {
					this.stmt = this.conn.createStatement();
					this.rs = this.stmt.executeQuery(this.sql);
					if (rs.next()) {
						int c_id = rs.getInt("c_id");
						// 查询是否已经添加过该课程
						this.sql = "select * from coruse where c_id = '" + c_id;
						this.rs = this.stmt.executeQuery(this.sql);
					if (!rs.next()) {
						this.sql="insert into course(s_id,c_id,teacher,score) values('" + c_id + "','" + course.getC_name()+ "',"
									+ "'" + course.getTeacher() + "','"+course.getScore()+"')";
							this.stmt.executeUpdate(this.sql);
							return true;
						}
					}
				} catch (SQLException e) {
					e.printStackTrace();
				} finally {
				// 关闭连接
					DataBaseConnection.closeRs(this.rs);
					DataBaseConnection.closeStmt(this.stmt);
					DataBaseConnection.closeConn(this.conn);
				}

			
			return false;
		}
		public boolean modifCourseinfo(Course course){
			// TODO Auto-generated method stub
			int rs=0;
			this.sql="update set teacher="+course.getTeacher()+"and score="+course.getScore()+"where c_id="+course.getC_id();
			this.conn=DataBaseConnection.getConnection();
			try {stmt=this.conn.createStatement();
			rs=stmt.executeUpdate(this.sql);
			if(rs>0){
				return true;
			}
			}catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeStmt(this.stmt);
				DataBaseConnection.closeConn(this.conn);
			}
			return false;
		}
	
		public int deleteCourseinfo(int id) throws Exception {
			// TODO Auto-generated method stub
			int rs=0;
			sql="delete from course where c_id="+id;
			this.conn=DataBaseConnection.getConnection();
			try{
			this.stmt=this.conn.createStatement();
			rs=this.stmt.executeUpdate(sql);
			}catch (SQLException e) {
				e.printStackTrace();
			} finally {
				// 关闭连接
				DataBaseConnection.closeStmt(this.stmt);
				DataBaseConnection.closeConn(this.conn);
			}
			return rs;
		}

		

	
	
	}
